package com.tian.arch.util

import android.os.SystemClock
import android.view.View

/**
 *  限制快速点击的Listener
 */
class LimitFastClickListener(private val duration: Long, private val next: (View) -> Unit) : View.OnClickListener {

    private var lastClickTime = 0L

    override fun onClick(v: View) {
        val diff = SystemClock.elapsedRealtime() - lastClickTime
        if (diff > duration) {
            next(v)
            lastClickTime = SystemClock.elapsedRealtime()
        }
    }
}

fun View.onClick(duration: Long? = 1000, action: (View) -> Unit) {
    this.setOnClickListener(LimitFastClickListener(duration ?: 1000, action))
}


